﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FrameworkManapro.BusinessModel;
using FrameworkManapro.WebUI;
using GMAC.ReportingSystem.Business.Entities;
using System.Collections;
using System.Data;

namespace GMAC.ReportingSystem.DataAccess
{
    //--------------------------------------------------------
    /// <summary>
    /// Ricardo Recaredo (23/03/2011)
    /// 
    /// DAO for HistoricRePurchaseDataAccess
    /// 
    /// </summary>
    //-------------------------------------------------------- 

    public class HistoricRePurchaseDataAccess : BaseDataAccess<Business.Entities.HistoricRePurchase>
    {
        #region Overrides for BaseDataAccess

        //-----------------------------------------------------

        public override GMAC.ReportingSystem.Business.Entities.HistoricRePurchase Get(object year)
        {
            ArrayList parameters = new ArrayList() { year };

            DataTable dt = DataAccessComponent.ExecuteSelect("procHistoricRePurchaseByYeaySelect", parameters);

            if (dt != null && dt.Rows.Count > 0)
            {
                return ReadRow(dt.Rows[0]);
            }

            return null;
        }

        //-----------------------------------------------------

        protected override GMAC.ReportingSystem.Business.Entities.HistoricRePurchase ReadRow(System.Data.DataRow row)
        {
            HistoricRePurchase historicRePurchase = new HistoricRePurchase();

            if (row != null)
            {
                try
                {
                    historicRePurchase.Id = int.Parse(row["Id"].ToString());
                    historicRePurchase.LiquidationYear = int.Parse(row["LiquidationYear"].ToString());
                    historicRePurchase.PercentageApplied = decimal.Parse(row["PercentageApplied"].ToString());
                    historicRePurchase.Repurchase = decimal.Parse(row["Repurchase"].ToString());
                    historicRePurchase.TotalBsCleared.val = decimal.Parse(row["TotalBsCleared"].ToString());
                    historicRePurchase.TotalCreditCleared = decimal.Parse(row["TotalCreditCleared"].ToString());
                }
                catch (Exception
                    )
                {
                    throw;
                }
            }
            return historicRePurchase;
        }

        //----------------------------------------------------------------------

        protected override List<HistoricRePurchase> ReadTable(System.Data.DataTable table)
        {
            try
            {
                HistoricRePurchaseCollection historicPurchases = new HistoricRePurchaseCollection();
                HistoricRePurchase historicPurchase = new HistoricRePurchase();

                if (table != null && table.Rows.Count > 0)
                {
                    foreach (DataRow row in table.Rows)
                    {
                        historicPurchase = ReadRow(row);

                        if (historicPurchase != null)
                        {
                            historicPurchases.Add(historicPurchase);
                        }
                    }
                }
                return historicPurchases;
            }
            catch (Exception)
            {
                throw;
            }
        }

        #endregion

        ////////////////////////////////////////////////////////////////

        #region Public Methods

        public CallReasonByAgentCollection GetList()
        {
            throw new NotImplementedException();
        }

        //-----------------------------------------------------

        public Years GetListOfAvailableYears()
        {
            var dtYears = DataAccessComponent.ExecuteSelect("procListAvilableYearsFromHistoricPurchases", null);

            return (Years)dtYears.AsEnumerable().Select(x => int.Parse(x["hyear"].ToString())).CreateFromEnumerable<Years, int>();
        }

        //-----------------------------------------------------

        public Years GetListOfAllYears()
        {
            var dtYears = DataAccessComponent.ExecuteSelect("procListAllYearsFromHistoricPurchases", null);

            return (Years)dtYears.AsEnumerable().Select(x => int.Parse(x["hyear"].ToString())).CreateFromEnumerable<Years, int>();
        }

        //----------------------------------------------------------------------

        public HistoricRePurchaseCollection Search(int? year, PagingDetails paging)
        {
            ArrayList parameters = new ArrayList() { year, paging.PageNumber, paging.PageSize };

            try
            {
                DataSet ds = DataAccessComponent.ExecuteMultipleSelect("procHistoricRePurchasesByFilterSelect", parameters);

                HistoricRePurchaseCollection result = null;

                if (ds.Tables.Count == 2)
                {
                    result = ReadTable(ds.Tables[0]) as HistoricRePurchaseCollection;

                    if (ds.Tables[1].Rows.Count > 0)
                    {
                        result.TotalResults = (int)ds.Tables[1].Rows[0][0];
                    }
                }
                return result;
            }
            catch (Exception)
            {
                throw;
            }
        }

        #endregion
    }

    ////////////////////////////////////////////////////////////////

    public static class Extensiones
    {
        #region Static Methods

        public static TDerived CreateFromEnumerable<TDerived, T>(this IEnumerable<T> seq) where TDerived : List<T>, new()
        {
            TDerived outList = new TDerived();

            outList.AddRange(seq);
           
            return outList;
        }

        #endregion
    }
}
